<?php
/**
 * @version		$Id: categories.php 18695 2010-08-28 17:18:48Z infograf768 $
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

/**
 * This models supports retrieving lists of article categories.
 *
 * @package		Joomla.Administrator
 * @subpackage	com_content
 * @since		1.6
 */
class jetextModelTexts extends JModelList
{
	/**
	 * Model context string.
	 *
	 * @var		string
	 */
	public $_context = 'com_jetext.texts';

	/**
	 * The category context (allows other extensions to derived from this model).
	 *
	 * @var		string
	 */
	protected $_extension = 'com_jetext';

	private $_items = null;

	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @since	1.6
	 */
	protected function populateState()
	{
		$app = JFactory::getApplication();

		// List state information
		//$value = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'));
		//$value = JRequest::getInt('limit', $app->getCfg('list_limit', 0));
		$this->setState('list.limit', $value = 20);

		//$value = $app->getUserStateFromRequest($this->context.'.limitstart', 'limitstart', 0);
		$value = JRequest::getInt('limitstart', 0);
		$this->setState('list.start', $value);

		// List state information.
		//parent::populateState('description', 'asc');
	}
	
	/**
	 * @param	boolean	True to join selected foreign information
	 *
	 * @return	string
	 * @since	1.6
	 */
	function getListQuery()
	{
		// Create a new query object.
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		// Select the required fields from the table.
		$query->select('a.*, org_info.name AS org_title, type_info.name AS type_title, working_kind_info.name as working_kind_title');
		$query->from('#__jetext AS a');
		$query->where('a.state = 1');
		$query->order('a.text_created desc');
		
		$query->join('LEFT', '#__jetext_info AS org_info ON org_info.id = a.org_id');		
		$query->join('LEFT', '#__jetext_info AS type_info ON type_info.id = a.type_id');
		$query->join('LEFT', '#__jetext_info AS working_kind_info ON working_kind_info.id = a.working_kind_id');
		
		$get = JRequest::get('get');
		
		if(@$get['text_code'])
			$query->where('a.text_code LIKE "%'.@$get['text_code'].'%"');
			
		if(@$get['text_created'])
			$query->where('a.text_created = "'.@$get['text_created'].'"');
			
		if(@$get['still_valid'] == 1)
			$query->where('a.still_valid = 1');
		else
			if(@$get['is_search'] == 1)
			$query->where('a.still_valid = 0');
			
		if(@$get['description'])
			$query->where('a.description LIKE "%'.@$get['description'].'%"');
			
		if(@$get['catid'])
			$query->where('a.catid = "'.@$get['catid'].'"');
			
		if(@$get['org_id'])
			$query->where('a.org_id = "'.@$get['org_id'].'"');

		if(@$get['type_id'])
			$query->where('a.type_id = "'.@$get['type_id'].'"');
			
		if(@$get['kind_id'])
			$query->where('a.working_kind_id = "'.@$get['kind_id'].'"');
		
		return $query;
	}

	/**
	 * Method to get a store id based on model configuration state.
	 *
	 * This is necessary because the model is used by the component and
	 * different modules that might need different sets of data or different
	 * ordering requirements.
	 *
	 * @param	string		$id	A prefix for the store id.
	 *
	 * @return	string		A store id.
	 */
	protected function getStoreId($id = '')
	{
		// Compile the store id.
		$id	.= ':'.$this->getState('filter.extension');

		return parent::getStoreId($id);
	}
	
	function getInfo($type = 'org')
	{
		$db =& JFactory::getDbo();
		$query = "SELECT * FROM #__jetext_info WHERE info_type = '$type' AND state = 1";
		
		$db->setQuery($query);
		
		return $db->loadObjectList();
	}
}
